function RobotGroupController(
    groupRobotList,//路由中预加载数据的注入
    $scope,
    $rootScope,
    RobotGroup,
    $uibModal,
    $state,
    $stateParams
) {
    var vm = this;
    var saveGroupInfoBtn = angular.element("#saveGroupInfoBtn");
    saveGroupInfoBtn.prop('disabled',true);

    groupRobotList.$promise.then(function(response) {
        vm.robotGroupDetail=response.result;
        vm.groupName = vm.robotGroupDetail.groupName;
        if(vm.robotGroupDetail.robotNumber > 0){
            vm.isLoaded = true;
            vm.queryParams = _.clone($stateParams)
            vm.initRobotPage();
        }
    })

    //监听分组名称input 防止多次修改相同名称
    vm.nameChange = function(){
        if(vm.groupName == vm.robotGroupDetail.groupName || vm.robotGroupDetail.groupName == undefined || vm.robotGroupDetail.groupName == ''){
            saveGroupInfoBtn.prop('disabled',true);
        }else{
            saveGroupInfoBtn.prop('disabled',false);
        }
    }


    vm.initRobotPage = function(){
        vm.cleanRobots();
        RobotGroup.findRobotPage({
            groupId : vm.robotGroupDetail.groupId,
            pageInfo: {
                pageSize: vm.queryParams.size,
                pageNumber: vm.queryParams.page
            }
        }).$promise.then(function(response){
            vm.robots=response.result.content;
            vm.totalItems = response.result.totalElements;
            vm.totalPages = response.result.totalPages;
        })
    }

    vm.cleanRobots = function(){
        vm.robots = null;
    }

    vm.reset = function(parameter) { vm.queryParams = {}; }

    vm.search = function() { $state.go($state.current, vm.queryParams) }

    vm.delete = function(robot) {
        vm.deleteRobotId = robot.robotNumber;
        var modalInstance = $uibModal.open({
            templateUrl: 'myModalContent.html',
            controller: 'deleteRobotIdCtrl'
        });
        var resultMsg;
        var scope = $rootScope.$new();
        // 模拟窗口关闭后回传调用
        modalInstance.result.then(function (selectedItem) {
            if(selectedItem == 'ok') {
                RobotGroup.deleteRobot({
                    groupId: vm.robotGroupDetail.groupId,
                    robotId : robot.id
                }).$promise.then(function(response){
                    if(response.result == "1"){
                        $state.reload($state.current)
                    }else if(response.result == "0"){
                        resultMsg = "删除失败，请稍后再试！";
                        scope.data = {
                            msg:resultMsg
                        }
                        var tipsModel = $uibModal.open({
                            templateUrl: 'tipsModel.html',
                            controller: 'robotGroupTipsCtrl',
                            scope:scope
                        });
                    }
                })
            }
        });
    }


    vm.saveRobotGroupInfo = function(){
        var resultMsg;
        var scope = $rootScope.$new();
        var keepGoing = false;


        if(vm.robotGroupDetail.groupName == undefined){
            alert("请输入分组名称！！！");
            return false;
        }else{
            saveGroupInfoBtn.prop('disabled',true);
            RobotGroup.saveRobotGroupInfo({
                groupId: vm.robotGroupDetail.groupId,
                groupName : vm.robotGroupDetail.groupName
            }).$promise.then(function(response){
                if(response.result == "1"){
                    $state.reload($state.current)
                }else if(response.result == "err"){
                    resultMsg = response.msg;
                    keepGoing = true;
                }else{
                    resultMsg = "保存失败，请稍后再试！";
                    keepGoing = true;
                }
                if(keepGoing){
                    scope.data = {
                        msg:resultMsg
                    }
                    var tipsModel = $uibModal.open({
                        templateUrl: 'tipsModel.html',
                        controller: 'robotGroupTipsCtrl',
                        scope:scope
                    });
                }
                saveGroupInfoBtn.prop('disabled',false);
            })
        }
    }

    vm.addRobotToGroup = function() {
        $state.go('addRobotInGroup', {groupId:vm.robotGroupDetail.groupId});
    }

}

